{block name=body}
{function name=linking_list data=$data level=0 levelcount=0 order='5' table='tbl_additional_category' dfield='additional_category_product_id' dvalue='product_id' dvalue_val= $fields.product_id pkey='additional_category_id' skey='additional_category_listing_id' flag='additional_category_flag' preselectedArr=$preselectedArr existingArr=$fields.additional_category}
	{foreach $data as $opt}
		{if $count eq ""}
			{if $level eq 0}
<input type="hidden" value="{$dvalue}" name="default[{$dfield}]" />
			{/if}
			{assign var=count value=1} 
		{else}
			{assign var=count value=$count+1} 
		{/if}
<div class="row form-group">
    <input type="hidden" value="{$pkey}" name="field[{$order}][{$table}][{$levelcount*10}{$count}][id]" />
    <input type="hidden" value="{call name=fn_pkey id=$opt.id existingArr=$existingArr}" name="field[{$order}][{$table}][{$levelcount*10}{$count}][{$pkey}]" class="key" />
    <input type="hidden" value="{$dvalue_val}" name="field[{$order}][{$table}][{$levelcount*10}{$count}][{$dfield}]" class="key">
    <input type="hidden" value="{$opt.id}" name="field[{$order}][{$table}][{$levelcount*10}{$count}][{$skey}]">
    <div class="col-sm-offset-3 col-sm-{$level+1}">
        <input type="hidden" value="{call name=fn_value id=$opt.id existingArr=$existingArr preselectedArr=$preselectedArr}" name="field[{$order}][{$table}][{$levelcount*10}{$count}][{$flag}]" class="value">
        <input style="float:right;" class="chckbx" type="checkbox" {call name=fn_selection id=$opt.id existingArr=$existingArr preselectedArr=$preselectedArr} onclick="if($(this).is(':checked')){ $(this).parent().children('.value').val('1') }else{ $(this).parent().children('.value').val('0') }" id="id_checkbox{$opt.id}">
    </div>
    <label style="text-align:left;" class="col-sm-3 control-label" for="id_checkbox{$opt.id}">{$opt.value}</label>
</div>
		{if count($opt.subs) > 0}
		{call name=linking_list data=$opt.subs level=$level+1 levelcount=$levelcount+{$count} }
		{/if} 
	{/foreach} 
{/function}

{function name=fn_value id="" existingArr="" preselectedArr = ""}
	{assign var=val value=0}
	{if $id neq ""}
		{foreach $existingArr as $add}
			{if $id eq $add.$skey}
				{assign var=val value=$add.$flag}
				{break}
			{/if}
			{if $id|in_array:$preselectedArr}
				{assign var=val value=1}
				{break}
			{/if}
		{/foreach}
	{/if}
	{$val}
{/function}

{function name=fn_selection id="" existingArr="" preselectedArr = ""}
	{if $id neq ""}
		{foreach $existingArr as $add}
			{if $id eq $add.$skey}
				{if $add.$flag eq 1}checked="checked"{/if}
				{break}
			{/if}
			{if $id|in_array:$preselectedArr}checked="checked"{break}{/if}
		{/foreach}
	{/if}
{/function}

{function name=fn_pkey id="" existingArr=""}
	{if $id neq ""}
		{foreach $existingArr as $add}
			{if $id eq $add.$skey}{$add.$pkey}{break}{/if}
		{/foreach}
	{/if}
{/function}
{assign var='sameFranchisee' value=0}
{foreach $fields.accesses as $a}
	{if $a.access_franchisee_id|in_array:$admin.franchisee }
		{assign var='sameFranchisee' value=1} {break}
	{/if}
{/foreach}
{if $sameFranchisee eq 1 || $admin.level eq 1 || !$fields.admin_id}
<div class="row">
    <div class="col-sm-12">
        <form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post" autocomplete=off>
            <div class="row">
                <div class="col-sm-12 edit-page-header">
                    <span class="edit-page-title">{if $fields.admin_id neq ""}编辑{else}新建{/if} {$zone} {if $cnt eq ""}{assign var=cnt value=0}{/if}</span>
                    <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right" style="margin-left: 38px;"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                    <input type="hidden" value="admin_id" name="primary_id" id="primary_id" />
                    <input type="hidden" value="{$fields.admin_id}" name="field[1][tbl_admin][{$cnt}][admin_id]" id="admin_id" />
                    <input type="hidden" value="admin_id" name="field[1][tbl_admin][{$cnt}][id]" id="id" />
                    <input type="hidden" value="{$fields.admin_username}" name="field[1][tbl_admin][{$cnt}][admin_username]" id="admin_username">
                    <input type="hidden" value="{$fields.admin_password}" name="field[1][tbl_admin][{$cnt}][admin_password]" id="admin_password">
                    <input type="hidden" name="formToken" id="formToken" value="{$token}" />
                    <input type="hidden" id="error" name="error" value="0" />
                </div>
            </div>
            <ul class="nav nav-tabs" id="myTab">
                <li class="active"><a href="#details" data-toggle="tab">详情</a></li>
                <li><a href="#log" data-toggle="tab">日志</a></li>
            </ul>
            <div class="tab-content">
                <!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane active" id="details">
                    <div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_name">* 姓名</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.admin_name}" name="field[1][tbl_admin][{$cnt}][admin_name]" id="admin_name" required>
                                <span class="help-block"></span>
                            </div>
                        </div>
<!--                         <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_surname">Surname</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.admin_surname}" name="field[1][tbl_admin][{$cnt}][admin_surname]" id="admin_surname">
                                <span class="help-block"></span>
                            </div>
                        </div> -->
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_email">* 邮箱</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="email" value="{$fields.admin_email}" name="field[1][tbl_admin][{$cnt}][admin_email]" id="admin_email" onchange="$('#admin_username').val(this.value);createPassword();" autocomplete=off required>
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_telephone">电话</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.admin_telephone}" name="field[1][tbl_admin][{$cnt}][admin_telephone]" id="admin_telephone">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="password">* 密码</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="password" value="" name="field1" id="password" onchange="createPassword();" autocomplete="off">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_image_link">图片
                                <br><small>尺寸: 120px宽 * 130px高</small></label>
                            <div class="col-sm-9">
                                <input type="hidden" value="{$fields.admin_image}" name="field[1][tbl_admin][{$cnt}][admin_image]" id="admin_image_link" class="fileinput">
                                <span class="file-view" id="admin_image_path">{if $fields.admin_image}<a href="{$fields.admin_image}" target="_blank" >查看</a>{else}暂无{/if}</span>
                                <a href="javascript:void(0);" class="btn btn-info marg-5r" onclick="getFileType('admin_image','','');">选择文件</a>
                                <a href="javascript:void(0);" class="btn btn-info" onclick="$('#admin_image').val('');$('#admin_image_link').val('');$('#admin_image_path').html('None');$('#admin_image_preview').html('');">移除文件</a>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="admin_description">描述</label>
                            <div class="col-sm-5">
                                <textarea class="form-control" maxlength="300" rows="4" name="field[1][tbl_admin][{$cnt}][admin_description]" id="admin_description">{$fields.admin_description}</textarea>
                            </div>
                        </div>
                        <input type="hidden" value="3" name="field[1][tbl_admin][{$cnt}][admin_level]" id="admin_level"> {if $admin.level eq 1}
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="franchisee_id">加盟商</label>
                        </div>
                        {call name=linking_list data=$fields.options.franchisee level=0 levelcount=0 order='5' table='tbl_access' dfield='access_admin_id' dvalue='admin_id' dvalue_val= $fields.admin_id pkey='access_id' skey='access_franchisee_id' flag='access_flag' preselectedArr=$preselectedArr existingArr=$fields.accesses}
{else}
	{if !$fields.admin_id}
                        <input type="hidden" value="admin_id" name="default[access_admin_id]" />
                        <input type="hidden" value="access_id" name="field[15][tbl_access][{$cnt}][id]" id="id" />
                        <input type="hidden" value="{$fields.accesses.0.access_admin_id}" name="field[15][tbl_access][{$cnt}][access_admin_id]" id="access_admin_id" class="key">
                        <input type="hidden" value="{$fields.accesses.0.access_id}" name="field[15][tbl_access][{$cnt}][access_id]">
                        <input type="hidden" value="{$admin.franchisee.0}" name="field[15][tbl_access][{$cnt}][access_franchisee_id]" id="access_franchisee_id">
                        <input type="hidden" value="1" name="field[15][tbl_access][{$cnt}][access_flag]" id="access_flag">
    {/if}
{/if}
                        <div class="row form-group">
                            <div class="col-sm-8">
                                <a href="javascript:void(0);" onClick="setFormCallback('{$partnerno}')" class="btn btn-info pull-right top-btn"><span class="glyphicon glyphicon-envelope"></span> 保存并通知</a>
                            </div>
                        </div>
                    </div>
                </div>
                <!--===+++===+++===+++===+++===+++ LOG TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane" id="log">
                    <div class="row form" id="tags-wrapper">
                        <div class="col-sm-12">
                            {if $fields.logs}
                            <table class="table table-bordered table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>日期</th>
                                        <th>操作</th>
                                        <th>用户</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {foreach $fields.logs as $log}
                                    <tr>
                                        <td>{$log.log_created|date_format:"%d/%b/%Y %r"}</td>
                                        <td>{$log.log_action}</td>
                                        <td>{$log.admin_name}</td>
                                    </tr>
                                    {/foreach}
                                </tbody>
                            </table>
                            {else} 暂无日志. {/if}
                        </div>
                    </div>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-sm-offset-3 col-sm-9">
                    <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right" style="margin-top: 50px;"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                </div>
            </div>
        </form>
    </div>
</div>
{include file='jquery-validation.tpl'}
<script type="text/javascript">
var init_pass = "{if $fields.admin_password}{$fields.admin_password}{/if}";

$(document).ready(function() {
    $('#Edit_Record').validate({
        onkeyup: false
    });
    $('#admin_email').rules("add", {
        uniqueEmail: {
            id: "{if $fields.admin_id}{$fields.admin_id}{else}0{/if}",
            table: "admin",
            field: "#admin_id"
        }
    });
});

function createPassword() {
    if ($('#password').val() != '' && $('#admin_email').val() != '') {
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/createPass.php",
            cache: false,
            data: "username=" + $('#admin_email').val() + "&password=" + $('#password').val(),
            dataType: "json",
            success: function(res, textStatus) {
                try {
                    $('#admin_password').val(res.password);
                } catch (err) {}
            }
        });
    } else {
        $('#admin_password').val(init_pass);
    }
}

function setFormCallback(NUMBER) {
    $('#Edit_Record').attr('data-callback', 'SaveNotify').submit();
}

function SaveNotify() {
    var email = encodeURIComponent($('#admin_email').val());
    var admin_id = encodeURIComponent($('#admin_id').val());
    if (email && admin_id) {
        var datastring = 'action=NotifyFranchiseUser&email=' + email + '&admin_id=' + admin_id + '&formToken=' + $('#formToken').val();
        $('body').css('cursor', 'wait');
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/processes-email.php",
            cache: false,
            async: false,
            data: datastring,
            dataType: "html",
            success: function(data) {
                try {
                    var obj = $.parseJSON(data);
                    if (obj.response) {
                        $('#sent').slideDown();
                        setTimeout(function() {
                            $('#sent').slideUp();
                        }, 10000);
                    } else {
                        $('#error').slideDown();
                        setTimeout(function() {
                            $('#error').slideUp();
                        }, 10000);
                    }
                    $('#Edit_Record').removeAttr('data-callback');
                } catch (err) {
                    console.log('TRY-CATCH error');
                    $('#Edit_Record').removeAttr('data-callback');
                }
                $('body').css('cursor', 'default');
            },
            error: function() {
                $('#Edit_Record').removeAttr('data-callback');
                $('body').css('cursor', 'default');
                console.log('AJAX error');
            }
        });
    }
}
</script>
{/if}
{/block}
